package org.shop.weixin.servlet;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.shop.weixin.pojo.SNSUserInfo;
import org.shop.weixin.util.AdvancedUtil;
import org.shop.weixin.util.EmojiFilterUtils;
import org.shop.weixin.util.WeixinOauth2Token;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * 授权后的回调请求处理
 *
 * @author
 * @date 2013-11-12
 */

@Controller("weixinOAuthController")
@RequestMapping("/weixinOAuth")
public class OAuthServlet {

    private static Logger log = LoggerFactory.getLogger(OAuthServlet.class);
    
    /**配置的是否开启首次关注送优惠劵标示*/
    @Value("${firstfocus}")
    private boolean       firstFocus;
    
    /**微信appid，配置文件中读取*/
    @Value("${customerappid}")
    private String        customerappid;
    
    /**微信customerappsecret，配置文件中读取*/
    @Value("${customerappsecret}")
    private String        customerappsecre;

    @RequestMapping(method = RequestMethod.GET)
    public String doGet(HttpServletRequest request, HttpServletResponse response, ModelMap model, HttpSession session) throws ServletException, IOException {

        
        System.out.println(firstFocus);
        System.out.println(customerappid);
        System.out.println(customerappsecre);
        String code = request.getParameter("code");
        log.info("进入商城端授权页面--------接收到的code数据--" + code);
        log.info("进入商城端授权页面--------验证是否同意授权--" + (!"authdeny".equals(code)));
        //
        // 用户同意授权
        if (!"authdeny".equals(code)) {
            // 获取网页授权access_token

            log.info("进入商城端授权页面--------用户同意授权的code数据--" + code);
            WeixinOauth2Token weixinOauth2Token = AdvancedUtil.getOauth2AccessToken(customerappid, customerappsecre, code);
            // 网页授权接口访问凭证
            String accessToken = weixinOauth2Token.getAccessToken();
            log.info("进入商城端授权页面--------用户同意授权的code数据获取网页授权接口访问凭证--" + accessToken);
            // 用户标识
            String openId = weixinOauth2Token.getOpenId();
            log.info("进入商城端授权页面--------用户openID--" + openId);
            // 获取用户信息
            SNSUserInfo snsUserInfo = AdvancedUtil.getSNSUserInfo(accessToken, openId);
            log.info("进入商城端授权页面--------用户昵称--" + snsUserInfo.getNickname());

            log.info("进入商城端授权页面--------从微信端返回的用户名为：" + EmojiFilterUtils.filterEmoji(snsUserInfo.getNickname()));

            //member验证和存入session           
            String toUri = request.getParameter("to");

            log.info("进入商城端授权页面--------用户openID会员登录方法验证成功--前往的页面为--" + toUri);
           
         if(true/*，根据snsUserInfo.getopenid()查询会员是否存在，会员存在*/ ){
             //跳转到请求的跳转页面
             return "redirect:/" + toUri;
         }else {
                //如果用户不存在，新增微信用户，和存入session，用户信息从snsUserInfo读取
                //跳转商城主页
             
                return "redirect:/index.html";

            }
        } 

        //未授权，跳转到未授权页面

        return "redirect:/unOAuth.jsp";
    }

}
